module traffic(
    input clk_1,
    input reset,
    input[7:0] counter_33,               //33秒计数器
    output reg Start_timer_33,          //开始计数信号
    output reg [1:0] H_display_en,      //横向道路倒计时显示使能
    output reg [7:0] H_count,           //横向道路倒计时数值
    output reg [1:0] V_display_en,      //纵向道路倒计时显示使能
    output reg [7:0] V_count,           //纵向道路倒计时数值
    output reg H_light_green,           //横向绿灯
    output reg H_light_yellow,          //横向黄灯
    output reg H_light_red,             //横向红灯
    output reg V_light_green,           //纵向绿灯
    output reg V_light_yellow,          //纵向黄灯
    output reg V_light_red              //纵向红灯
    );
    parameter H_green   = 4'b0001;
    parameter H_yellow  = 4'b0010;
    parameter V_green   = 4'b0100;
    parameter V_yellow  = 4'b1000;
    parameter YES = 1'b1;
    parameter NO = 1'b0;
    parameter ON  = 1'b1;
    parameter OFF = 1'b0;
    parameter DISPLAY_ON = 2'b00; // Assuming 2'b00 means ON, based on usage in reset block
    parameter DISPLAY_OFF = 2'b11; // Assuming 2'b11 means OFF, based on usage in reset block

    reg [3:0] state;
    reg [3:0] next_state;

//第一个进程，同步时序always模块，格式化描述次态寄存器转移到现态寄存器；
    always@(posedge clk_1 ,negedge reset)
        begin
            if(!reset)
                state <= H_green;
            else
                state <= next_state;
        end

//第二个进程，同步时序always模块，描述次态寄存器的状态转移条件
    always@ (posedge clk_1 ,negedge reset)
        if(!reset)
             next_state <= H_green;
        else
        begin
            case(next_state)
                H_green:
                    if(counter_33 == 4)
                        next_state <= H_yellow;
                    else
                        next_state <= H_green;
                H_yellow:
                    if(counter_33 == 1)
                        next_state <= V_green;
                    else
                        next_state <= H_yellow;
                V_green: // 补全 V_green 状态的次态转移
                    if(counter_33 == 4) // 假设 V_green 到 V_yellow 的转移条件对称于 H_green
                        next_state <= V_yellow;
                    else
                        next_state <= V_green;
                V_yellow: // 补全 V_yellow 状态的次态转移
                	if(counter_33 == 1) // 假设 V_yellow 到 H_green 的转移条件对称于 H_yellow
                        next_state <= H_green;
                    else
                        next_state <= V_yellow;
                default:
                    next_state <= H_green;
            endcase
        end


//第三个进程，同步时序always块，格式化描述次态寄存器的输出
    always@(posedge clk_1 ,negedge reset)
        if(!reset)
            begin
                H_light_green <= ON;
                H_light_yellow <= OFF;
                H_light_red <= OFF;
                V_light_green <= OFF;
                V_light_yellow <= OFF;
                V_light_red <= ON;
                H_count <= 8'b00000000; // Changed to 8-bit to match declaration
                V_count <= 8'b00000000; // Changed to 8-bit to match declaration
                H_display_en <= DISPLAY_OFF;
                V_display_en <= DISPLAY_OFF;
                Start_timer_33 <= YES;
            end
        else
            begin
                case(next_state)
                    H_green:
                        begin
                            H_light_green <= ON;
                            H_light_yellow <= OFF;
                            H_light_red <= OFF;
                            V_light_green <= OFF;
                            V_light_yellow <= OFF;
                            V_light_red <= ON;
                            Start_timer_33 <= NO;
                            if(counter_33 <= 18 && counter_33 >= 4)
                                begin
                                    H_count <= counter_33 - 3;
                                    H_display_en <= DISPLAY_ON;
                                end
                            else
                                H_display_en <= DISPLAY_OFF;
                            if(counter_33 <= 15 && counter_33 >= 1)
                                begin
                                    V_count <= counter_33;
                                    V_display_en <= DISPLAY_ON;
                                end
                            else
                                V_display_en <= DISPLAY_OFF;
                        end
                    H_yellow:
                        begin
                            H_light_green <= OFF;
                            H_light_yellow <= ON;
                            H_light_red <= OFF;
                            V_light_green <= OFF;
                            V_light_yellow <= OFF;
                            V_light_red <= ON;
                            if(counter_33 <= 3 && counter_33 >= 1)
                                begin
                                    H_count <= counter_33;
                                    H_display_en <= DISPLAY_ON;
                                end
                            else
                                H_display_en <= DISPLAY_OFF;
                            // Following the pattern of V_count in H_green, though its exact timing meaning is unclear
                            if(counter_33 <= 15 && counter_33 >= 1)
                                begin
                                    V_count <= counter_33;
                                    V_display_en <= DISPLAY_ON;
                                end
                            else
                                V_display_en <= DISPLAY_OFF;
                            if(counter_33 == 1)
                                Start_timer_33 <= YES;
                            else
                                Start_timer_33 <= NO;
                        end
                    V_green: // 补全 V_green 状态的输出
                        begin
                            H_light_green <= OFF; // 横向红灯
                            H_light_yellow <= OFF;
                            H_light_red <= ON;
                            V_light_green <= ON; // 纵向绿灯
                            V_light_yellow <= OFF;
                            V_light_red <= OFF;
                            Start_timer_33 <= NO; // 在进入 V_green 时定时器已经启动，此处不启动
                            // 纵向绿灯倒计时，对称于 H_green 的 H_count
                            if(counter_33 <= 18 && counter_33 >= 4)
                                begin
                                    V_count <= counter_33 - 3;
                                    V_display_en <= DISPLAY_ON;
                                end
                            else
                                V_display_en <= DISPLAY_OFF;
                            // 横向红灯倒计时，对称于 H_green 的 V_count
                            if(counter_33 <= 15 && counter_33 >= 1)
                                begin
                                    H_count <= counter_33;
                                    H_display_en <= DISPLAY_ON;
                                end
                            else
                                H_display_en <= DISPLAY_OFF;
                        end
                    V_yellow: // 补全 V_yellow 状态的输出
                        begin
                            H_light_green <= OFF; // 横向红灯
                            H_light_yellow <= OFF;
                            H_light_red <= ON;
                            V_light_green <= OFF; // 纵向黄灯
                            V_light_yellow <= ON;
                            V_light_red <= OFF;
                            // 纵向黄灯倒计时，对称于 H_yellow 的 H_count
                            if(counter_33 <= 3 && counter_33 >= 1)
                                begin
                                    V_count <= counter_33;
                                    V_display_en <= DISPLAY_ON;
                                end
                            else
                                V_display_en <= DISPLAY_OFF;
                            // 横向红灯倒计时，对称于 H_yellow 的 V_count (可能与 V_green 的 H_count 重叠，但遵循已有模式)
                             if(counter_33 <= 15 && counter_33 >= 1)
                                begin
                                    H_count <= counter_33;
                                    H_display_en <= DISPLAY_ON;
                                end
                            else
                                H_display_en <= DISPLAY_OFF;
                            // 在 V_yellow 结束时启动定时器，以便进入下一状态 H_green
                            if(counter_33 == 1)
                                Start_timer_33 <= YES;
                            else
                                Start_timer_33 <= NO;
                        end
                    default:
                        begin
                            H_light_green <= ON;
                            H_light_yellow <= OFF;
                            H_light_red <= OFF;
                            V_light_green <= OFF;
                            V_light_yellow <= OFF;
                            V_light_red <= ON;
                            H_count <= 8'b00000000; // Changed to 8-bit
                            V_count <= 8'b00000000; // Changed to 8-bit
                            H_display_en <= DISPLAY_OFF;
                            V_display_en <= DISPLAY_OFF;
                            Start_timer_33 <= YES;
                        end
                endcase
            end

endmodule
